<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>其他出库编辑</title>
<!-- import css and js -->
<%@ include file="/common/metaInput.jsp"%>
<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${ctx}/js/jsp/yd/logic/ItemStateData.js"></script>
<script src="${ctx}/js/jsp/date.js" type="text/javascript"></script>
<script type="text/javascript">
	var obj= new Object();
	var basic_Url = "${ctx}/logic/otherOutwh";
	var addressGrid;
	var print=false;
	$(function() {
		obj={'zd013RunningNumber':$('#zd013RunningNumber').val(),'zd011CustomerName':$('#zd011CustomerName').val(),'zd012PiCode':$('#zd012PiCode').val(),'zd004WhName':$('#zd004WhName').val(),'zd010OtherDate':$('#zd010OtherDate').val(),'zd001OutCode':$('#zd001OutCode').val(),'zd006Aegnt':'${userName}'};
		$("#inputForm").validate({
			onsubmit : true,
			/*执行ajaxsubmit  */
			submitHandler : function(editform) {

				//alert(jsonInfo); 
				try {
					url = basic_Url + "/save.do";
					var api = frameElement.api;
					var W = api.opener;
					//getReturnMessage(url, "keinputForm", W.searchAP);
					getReturnMessageThree(url, "inputForm", W.searchAP,"",obj,getNeedData(addressGrid.getData()),print,"otherOut");
				} catch (e) {
					alert(e);
				}
			}
		});
		var zd003BillStatus=$("#zd003BillStatus").val();
		if ('${viewflag}' == "add"||zd003BillStatus=="OTHERWH_BACK"){
			$("#save1").hide();
			$("#save2").hide();
			$("#save3").hide();
			$("#tr1").hide();
			$('#print1').hide();
			$('#print2').hide();
		}else if ('${viewflag}' == "tijiao"){
			$("#save").hide();
			$("#save2").hide();
			$("#save3").hide();
            if($("#zd006Aegnt").val()!="${user.name}"){
    			$("#save1").hide();
    			$("input[type!='hidden']").attr("disabled",true);//禁用SELECT标签
    			$("select").attr("disabled",true);//禁用SELECT标签
    			$("textarea").attr("disabled",true);//禁用TEXTAREA标签
               }  
		}else if ('${viewflag}' == "shenpi"){
			$("#save").hide();
			$("#save1").hide();
			$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
			$("select").attr("disabled","disabled");//禁用SELECT标签
			$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
		}else if ('${viewflag}' == "tongguo"){
			$("#save1").hide();
			$("#save2").hide();
			$("#save3").hide();
			$("#save").hide();
			$("input[type!='hidden']").attr("disabled","disabled");//禁用SELECT标签
			$("select").attr("disabled","disabled");//禁用SELECT标签
			$("textarea").attr("disabled","disabled");//禁用TEXTAREA标签
		}
		
		//zd012PiCode
		//zd012PiCode改变
		$("#zd012PiCode").change(function(){
			var piCode = $("#zd012PiCode").val();
			if(piCode==null||piCode==''){
				return;
			}
			$.ajax({
				url:"${ctx}/logic/sales/getCustomer.do",
				global: false,
				type:"POST",
				dataType:"json",
				data:({"picode":piCode}),		     
	  		    success: function(msg){
	  		    	if(msg.msg=="success"){
						$("#zd011CustomerID").val(msg.customerId);
						$("#zd011CustomerName").val(msg.customerName);
						$("#zd011CustomerCode").val(msg.customerCode);
					}else{
						$("#zd011CustomerID").val("");
						$("#zd011CustomerName").val("");
						$("#zd011CustomerCode").val("");
						if(msg.msg==0){
							return;
						}else{
							$.dialog.alert("PI号出现重复，请检查确认后重新出入");
						}
					}
	  		    }
	  	    });	
		});
	});
	function submitForm(param) {
	 	$("#zd003BillStatus").val(param);
// 	 	$("#zd004WhName").val($("#zd004WhCode").find("option:selected").text());
		var dataInfo=$("#dataGrid1").ligerGetGridManager().getData();
		for(var i=0 ; i<dataInfo.length ;i++){
			dataInfo[i].zd020Order=i+"";
			if($("#zd004WhCode").val()=='0023'||$("#zd004WhCode").val()=='0014'){
				dataInfo[i].zd009Price=0;
			}
		}
	 	$("#addressInfos").val(JSON.stringify(dataInfo));
		var data = $("#dataGrid1").ligerGetGridManager().getData();
		if(data.length==0){
			$.dialog.alert("请增加物料信息");
	   	 	return false;
		}
		var ba = true;
		if(data !=null){ 
			for (var i = 0;i < data.length;i++){
				var row = data[i]; 
				if (undefined == row.zd006Unit|| null == row.zd006Unit||row.zd006Unit=="") {
					$.dialog.alert("第" + (i+1) + "行的单位没有选择");
					ba = false;
					return false;
				}
				if(undefined == row.zd014WhQuantity
						|| isNaN(row.zd014WhQuantity)
						|| row.zd014WhQuantity <= 0){
		           	 $.dialog.alert("您第"+(i+1)+"行的“库存数量”为0，无法出库");
		           	 ba=false;
		           	 return false;
		        }
				if(undefined == row.zd007Quantity
						|| isNaN(row.zd007Quantity)
						|| row.zd007Quantity <= 0){
		           	 $.dialog.alert("您第"+(i+1)+"行的“出库数量”不合法");
		           	 ba=false;
		           	 return false;
		        }
				if(row.zd007Quantity>row.zd014WhQuantity){
					$.dialog.alert("您第"+(i+1)+"行的“出库数量”大于“库存数量”无法出库");
		           	 ba=false;
		           	 return false;
				}
			}
		}
		//设置ajax全局同步
		jQuery.ajaxSettings.async = false;
		if(ba){
			$("#submitFlag").val(param);
			if(param=='OTHERWH_AUDIT'){
				print=true;
			}
			$("#inputForm").submit();
		}
	}
	
	function check(){
		$.ajax({
		      url: "${ctx}/logic/otherOutwh/updateState.do",
		      global: false,
		      type: "POST",
		      dataType: "json",
		      data: ({
		    	   rows:$("#id").val()
		      }),		     
		      success: function(msg){
		    	  if(msg.msg=="SUCCESS"){
		    		  $.dialog.alert("驳回成功",function(){close();});
		    	  }
		    	  if(msg.msg=="ERROR"){
		    		  $.dialog.alert("操作失败",function(){close();});
		    	  }
		   }
		});
	}
	
	function close(){
		var api = frameElement.api;
		var W = api.opener;
		CloseDialog();
		W.searchAP();
	}
    
	//出库仓库
	function  SotrageinfoWindow() {
		getDG().dialog(
				{
					title : '选择仓库',
					width : 650,
					height : 350,
					content : 'url:' + sys_context + '/public/windowalert/sotrageinfo.do?roleQuery=y',    
					lock : true,
					max : true,
					min : true,
					ok : function() {
						var rows = this.content.getSelected();
						if(rows==null){
							$.dialog.alert("请选择数据");
							return;
						}
						else{
							if(rows.length>1){
								$.dialog.alert('只能选择一行数据!');
								return;
							}else{
								$(rows).each(function() {
									$("#zd004Whid").val(this.id);
									$("#zd004WhCode").val(this.zd002Code);
									$("#zd004WhName").val(this.zd003Name);
							  });
							}
						}
						this.content.Close();
						return false;
					},
					cancelVal : '关闭',
					cancel : true
				}).zindex();
	}
	
	function selectCustomer(){
		//modify 14.10.30 新增其他出库选择客户名单
		getDG().dialog(
				{
					title : '选择客户',
					width : 650,
					height : 350,
					content : 'url:' + sys_context + '/public/windowalert/company.do?zdType='+'cust',    
					lock : true,
					max : true,
					min : true,
					ok : function() {
						var rows = this.content.getSelected();
						if(rows==null){
							$.dialog.alert("请选择数据");
							return;
						}else{
							if(rows.length>1){
								$.dialog.alert("只能选择一行数据!");
								return;
							}else{
								$(rows).each(function() {
									$("#zd011CustomerCode").val(this.zd001Code);
									$("#zd011CustomerName").val(this.zd003ZhName);
									$("#zd011CustomerID").val(this.id);
								});
							}
						}
						this.content.Close();
						return false;
					},
					cancelVal : '关闭',
					cancel : true
				}).zindex();
	}
	function PrintView(type){
		var isprint=false;
		if(type==2){
			isprint=true;
		}
		window.parent.parent.loadPrint(obj,getNeedData(addressGrid.getData()),"otherOut",isprint);
	}
	function getNeedData(data){
		var arr= new Array();
		for(var i=0;i<data.length;i++){
			if(!isNaN(data[i].zd007BackQuantity)){
				data[i].zd007Quantity=parseFloat(data[i].zd007Quantity)-parseFloat(data[i].zd007BackQuantity);
			}
			if(parseFloat(data[i].zd007Quantity)!=0){
				arr.push(data[i]);
			}
		}
		return arr;
	}
</script>

</head>

<body>

	<form id="inputForm" action="" method="post">
		<input type="hidden" id="id" name="id" value="${oi.id}" />
		<input type="hidden" id="addressInfos" name="addressInfos"/>
		<input type="hidden" id="dataItem" name="dataItem"/>
		<input type="hidden" id="batchItem" name="batchItem"/>
		<input type="hidden" id="bizVersion" name="bizVersion" value="${oi.bizVersion}"/>
		<input type="hidden" id="zd003BillStatus" name="zd003BillStatus" value="${oi.zd003BillStatus}"/>
		<!-- 用于标识提交按钮 -->
		<input type="hidden" id="submitFlag" value="" />
		<div class="pageContent">
			<div class="panel">
				<table class="TableBlock" align="center">
					<tr id="tr1">
					 	<td>出库单号:</td>
						<td >
							<input class="inp_width_98p"  type="text" id="zd001OutCode" name="zd001OutCode" readonly="readonly" value="${oi.zd001OutCode}" />
						</td>
						<td colspan="2"></td>
					</tr>
					<tr>
					 	<td>领料单流水号:</td>
						<td >
							<input class="inp_width_98p"  type="text" id="zd013RunningNumber" name="zd013RunningNumber"  value="${oi.zd013RunningNumber}" />
						</td>
						<td colspan="2"></td>
					</tr>
					<tr>
						<td>出库仓库:</td>
						<td>
				  		  <input type="hidden" id="zd004WhCode" name="zd004WhCode" value="${oi.zd004WhCode}"/>
   		                     <input type="text" id="zd004WhName" name="zd004WhName" value="${oi.zd004WhName}"/>
	   		            	 <input type="hidden" style="width:200px;" id="zd004Whid" name="zd004Whid" value="${oi.zd004Whid}" />
   		        			<input type="button" value="..." onclick="SotrageinfoWindow();" />
						</td>
					 	<td>出库时间:</td>
						<td><input id="zd010OtherDate" name="zd010OtherDate" class="inp_width_98p required" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" value="<fmt:formatDate value="${oi.zd010OtherDate}" pattern="yyyy-MM-dd" type="both" />" />
							<c:if test="${oi.zd010OtherDate == null}">
									<script>
										var date = new Date();
										$("#zd010OtherDate").val(date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate());
									</script>
							 </c:if>
						</td>
					</tr>
					<tr>
						<td>pi编号:</td>
						<td>
							<input type="text" id="zd012PiCode" name="zd012PiCode" value="${oi.zd012PiCode }" class="inp_width_98p required"/>
						</td>
						<td>制单人:</td>
						<td>
						    <input type="text" id="zd006Aegnt" name="zd006Aegnt" class="inp_width_98p" value="${user.name}" readonly="readonly"/>
							<c:if test="${oi.zd006Aegnt != null}">
								<script>
									$("#zd006Aegnt").val("${oi.zd006Aegnt}");
								</script>
							</c:if>
					  	</td>
					</tr>
					<tr>
						<td>客户:</td>
					 	<td>
					 		<input type="text" id="zd011CustomerName" name="zd011CustomerName" value="${oi.zd011CustomerName}" readonly="readonly"/>
					 		<input type="button" value="..." onclick="selectCustomer();"/>
					 		<input type="hidden" id="zd011CustomerID" name="zd011CustomerID"  value="${oi.zd011CustomerID}"/>
					 	</td>
						<td>客户编码:</td>
					 	<td>
					 		<input type="text" id="zd011CustomerCode" name="zd011CustomerCode" value="${oi.zd011CustomerCode}" readonly="readonly" class="required"/>
					 	</td>
					</tr>
					<tr>
						<td>备注:</td>
						<td colspan="3"><input id="zd007Remark" name="zd007Remark"
							class="inp_width_98p" value="${oi.zd007Remark}" /></td>
					</tr>
					<tr>
						<td colspan="4" style="margin-bottom: 10px;"><div
								id="dataGrid1"></div></td>
					</tr>
				</table>
			</div>
			<div class="formBar">
				<ul>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save" onclick="submitForm('OTHERWH_SAVE')">保存</button></div></div></li>
					 <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save1" onclick="submitForm('OTHERWH_AUDIT')">提交</button></div></div></li>
		        <webres:displaycontroller elementId="LOGIC_OUTWH_CK_SP" description="其他出库单审批">
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save2" onclick="submitForm('OTHERWH_END')">审核通过</button></div></div></li>
			    </webres:displaycontroller>
			    <webres:displaycontroller elementId="LOGIC_OUTWH_CK_PH" description="其他出库单驳回">   
			       <li><div class="buttonActive"><div class="buttonContent"><button type="button" id="save3" onclick="check()">驳回</button></div></div></li>
		       	</webres:displaycontroller>
				<li id='print1'><div class="buttonActive"><div class="buttonContent"><button type="button" id="forPrint" onclick="PrintView(1);">打印预览</button></div></div></li>
		      	<li id='print2'><div class="buttonActive"><div class="buttonContent"><button type="button" id="forPrint" onclick="PrintView(2);">打印</button></div></div></li>
		       <li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
				</ul>
			</div>
		</div>
		<script type="text/javascript">
		alert(1)
		var ItemStateList = ItemStateData.Rows;
    var column1 = [ { display: 'id', name: 'id',type: 'string',hide:true},
			    	{ display: '物料id', name: 'zd002MaterialsId',width:1,hide:true},//使用selecturl时必须设置该属性
			    	{ display: '<font color="gray">物料编码</font>', name: 'zd002MaterialsCode', width: 120 },
			    	{ display: '<font color="gray">名称</font>', name: 'zd002MaterialsName',width: 120},
			    	{ display: '<font color="gray">型号</font>', name: 'zd004MaterialsModel' ,width: 120},
			    	{ display: '<font color="gray">规格</font>', name: 'zd003MaterialsSpec' ,width: 120},
			    	{ display: '<font color="gray">颜色</font>', name: 'zd016MaterialsColor' ,width: 100},
			    	{ display: '<font color="gray">材质</font>', name: 'zd017MaterialsQuanlity' ,width: 100},
			    	{ display: '<font color="gray">基本单位</font>', name: 'zd006Unit' ,width: 100},
			    	{ display: '<font color="gray">库存数量</font>', name: 'zd014WhQuantity',width: 100 },
			    	{ display: '单价', name: 'zd009Price',width: 100,hide:true },
			    	{ display: '<font color="gray">pi编号</font>', name: 'zd018PiCode',width: 100 },
			    	{ display: '出库数量<font color="red">*</font>', name: 'zd007Quantity',editor : {type :'float'},width: 80,
			    		render:function(item){
			    			if(null==item.zd007Quantity||undefined==item.zd007Quantity||parseFloat(item.zd007Quantity)==0){
				    			item.zd007Quantity=item.zd014WhQuantity;
			    			}
			    			return item.zd007Quantity;
			    		}
			    	},
			    	{ display: '反审数量', name: 'zd007BackQuantity',width: 80,
    			   		render:function(item){
    			   			if(isNaN(item.zd007BackQuantity)){
    			   				return '0.00';
    			   			}else{
    			   				return item.zd007BackQuantity;
    			   			}
    			   		}},
			    	{ display: '客户名称', name: 'zd021CustomerName',width: 150},
			    	{ display: '客户id', name: 'zd021CustomerId',hide:true,width: 1},
			    	{ display: '客户编码', name: 'zd021CustomerCode',hide:true,width: 1},
			    	{ display: '描述', name: 'zd019MaterDecript',editor : {type :'text'},width: 150}
     			];
    var column2 = [ { display: 'id', name: 'id',type: 'string',hide:true},
				    { display: '物料id', name: 'zd002MaterialsId',width:1,hide:true},//使用selecturl时必须设置该属性
				    { display: '<font color="gray">物料编码</font>', name: 'zd002MaterialsCode', width: 120 },
                   	{ display: '<font color="gray">名称</font>', name: 'zd002MaterialsName',width: 120},
                   	{ display: '<font color="gray">型号</font>', name: 'zd004MaterialsModel' ,width: 120},
                   	{ display: '<font color="gray">规格</font>', name: 'zd003MaterialsSpec' ,width: 120},
			    	{ display: '<font color="gray">颜色</font>', name: 'zd016MaterialsColor' ,width: 100},
			    	{ display: '<font color="gray">材质</font>', name: 'zd017MaterialsQuanlity' ,width: 100},
                   	{ display: '<font color="gray">基本单位</font>', name: 'zd006Unit' ,width: 100},
    			   	{ display: '<font color="gray">库存数量</font>', name: 'zd014WhQuantity',width: 100 },
    			   	{ display: '单价', name: 'zd009Price',width: 1,hide:true },
    			   	{ display: '<font color="gray">pi编号</font>', name: 'zd018PiCode',width: 100 },
    			   	{ display: '出库数量<font color="red">*</font>', name: 'zd007Quantity',width: 80},
    			   	{ display: '反审数量', name: 'zd007BackQuantity',width: 80,
    			   		render:function(item){
    			   			if(isNaN(item.zd007BackQuantity)){
    			   				return '0.00';
    			   			}else{
    			   				return item.zd007BackQuantity;
    			   			}
    			   		}},
                   	{ display: '客户名称', name: 'zd021CustomerName',width: 150},
			    	{ display: '客户id', name: 'zd021CustomerId',hide:true,width: 1},
			    	{ display: '客户编码', name: 'zd021CustomerCode',hide:true,width: 1},
                   	{ display: '描述', name: 'zd019MaterDecript',editor : {type :'text'},width: 150}
	               ];
                
    var column3 = null;
    if ('${xx}' == 1){
    	column3 = column1;
    }else 
    	column3 = column2;
    
    addressGrid = $("#dataGrid1").ligerGrid({
		 columns: column3,
	                    enabledEdit : true,
	                    usePager: true,
	      				width : '99%',
	      				pageSize:1000,
	      				pageSizeOptions:[1000],
	      				parms:[{ name : 'id' , value : '${oi.id}'}],
      		        	url : basic_Url+"/griditems.do",
	      				rownumbers : false,
	      				checkbox : true,
	                    height : '90%',
		toolbar : {
			items : [ {
				text : '新增',
				disable : true,
				click : function() {
					if ('${xx}' == 1){
						if($("#zd004WhCode").val()==''){
							$.dialog.alert("请先选择出库仓库");
							return false;
						}else{
							getDG().dialog({
					    		title:'选择材料',
					    		width : 800,
								height : 400,
								content: 'url:' + sys_context+'/public/windowalert/itemaff.do?code=' + $("#zd004WhCode").val(),//
					    		lock : true,
					    		max : true,
					    		min : true,
					    		ok: function(){
					    			var rows = this.content.getSelected();
									if (rows != null) {
										for (var i = 0; i < rows.length;i++){
											var row = rows[i];
											addressGrid.addRow2({
												zd002MaterialsId : row.zd003MaterialsId,
												zd002MaterialsCode : row.zd004MaterialsCode,
												zd002MaterialsName : row.zd005MaterialsName,
												zd004MaterialsModel : row.zd007MaterialsModel,
												zd003MaterialsSpec : row.zd006MaterialsSpec,
												zd016MaterialsColor : row.zd008MaterialsColor,
												zd017MaterialsQuanlity : row.zd008MaterialsQuanlity,
												zd010Batch : row.zd020Batch,
												zd014WhQuantity : row.zd009Quantity,
												zd018PiCode : row.zd026PiCode,
	   											zd006Unit : row.zd008Unit,
	   											zd021CustomerId:row.zd028CustomerID ,
	   											zd021CustomerName:row.zd028CustomerName,
	   											zd021CustomerCode:row.zd028CustomerCode,
	   											zd009Price:row.zd024Price
	   											
											});
										}
									}
					    			this.content.Close();
					        		return false;
					    	    },
					    	    cancelVal: '关闭',
					    	    cancel: true 
					    	}).zindex();
						}
					}
				},
				icon: 'add'
			}, {
				line : true
			}, {
				text : '删除',
				click : function() {
					if ('${xx}' == 1){
					addressGrid.deleteSelectedRow();
					}
				},
				img : '${ctx}/js/ligerUI/skins/icons/delete.gif'
			}]
		}});
    </script>
	</form>
</body>
</html>
